package cn.xzq.crm.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.xzq.crm.dao.CustomerDao;
import cn.xzq.crm.dao.UserDao;
import cn.xzq.crm.dao.VisitDao;
import cn.xzq.crm.entity.Customer;
import cn.xzq.crm.entity.User;
import cn.xzq.crm.entity.Visit;
import cn.xzq.crm.execption.VisitException;

@Service("visitService")
@Transactional
public class VisitService {
	
	@Resource(name="visitDaoImpl")
	private VisitDao visitDao;
	@Resource(name="userDaoImpl")
	private UserDao userDao;
	@Resource(name="customerDaoImpl")
	private CustomerDao customerDao;
	
	/**
	 * 添加一个客户访问列表
	 * @param visit
	 * @throws VisitException 
	 */
	public void add(Visit visit) throws VisitException {
		//获取id并封装到visit里面
		Customer customer = customerDao.findOneById(visit.getCustomer().getCustId());
		User user = userDao.findOneById(visit.getUser().getUid());
		if(customer == null){
			throw new VisitException("没有找到相应的客户！");
		}
		if(user == null){
			throw new VisitException("没有找到相应的用户！");
		}
		visit.setCustomer(customer);
		visit.setUser(user);
		visitDao.add(visit);
	}

	/**
	 * 查找所有客户拜访记录
	 * @return
	 */
	public List<Visit> findAll() {
		return visitDao.findAll();
	}

	/**
	 * 根据ID获取一个Visit对象
	 * @param vid
	 * @return
	 */
	public Visit findOneById(String vid) {
		return visitDao.findOneById(vid);
	}

	/**
	 * 修改一个客户拜访记录
	 * @param visit
	 * @throws VisitException 
	 */
	public void update(Visit visit) throws VisitException {
		//获取id并封装到visit里面
		Customer customer = customerDao.findOneById(visit.getCustomer().getCustId());
		User user = userDao.findOneById(visit.getUser().getUid());
		if(customer == null){
			throw new VisitException("没有找到相应的客户！");
		}
		if(user == null){
			throw new VisitException("没有找到相应的用户！");
		}
		visit.setCustomer(customer);
		visit.setUser(user);
		visitDao.update(visit);
	}

	/**
	 * 多条件综合查询
	 * @return
	 */
	public List<Visit> findMoreCondition(Visit visit) {
		return visitDao.findMoreCondition(visit);
	}

	
}
